package com.codeczx.searchdemo;

import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * use setOutsideTouchable or setFocusable
 */
public class PopupActivity extends AppCompatActivity{

    private static final String TAG = "PopupActivity";
    RecyclerView rv;
    RVAdapter adapter;
    EditText et;
    Button btn;
    PopupWindow popupWindow;
    ListPopupWindow popup;
    List<String> strs;
    List<String> tmpStrs;

    private int ITEM_HEIGHT = 55;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup);

//                String[] strAry = {"a", "2", "3", "4", "5", "6", "7", "8", "9"};
//        et = (EditText) findViewById(R.id.et);
//        popup= new ListPopupWindow(this.getApplicationContext());
//        popup.setAdapter(new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,strAry));
//        popup.setAnchorView(et);
//        popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED);
//
//        et.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View eventView) {
//                if(!popup.isShowing()){
//                    popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED);
//                    popup.show();
//                }
//            }
//        });
//        et.addTextChangedListener(new TextWatcher(){
//            @Override
//            public void afterTextChanged(Editable arg0) {}
//            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
//                                          int arg3) {}
//            @Override
//            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//                if(!popup.isShowing()){
//                    popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED);
//                    popup.postShow();
//                }
//            }
//        });

        final View popupView = getLayoutInflater().inflate(R.layout.popupwindow,null);
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);

        rv = (RecyclerView) popupView.findViewById(R.id.rv);
        et = (EditText) findViewById(R.id.et);

        strs = new ArrayList<>();
        tmpStrs = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            strs.add("我是第"+i+"个item");
        }

        adapter = new RVAdapter(strs);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);

        et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick: et"+popupWindow.isShowing());
                if (!popupWindow.isShowing()) {
                    popupWindow.showAsDropDown(et);
                    if (!popupWindow.isShowing()) {
                        popupWindow.showAsDropDown(et);
                        if(et.getText().length()!=0){
                            tmpStrs.clear();
                            for (int i = 0; i < strs.size(); i++) {
                                if (strs.get(i).contains(et.getText())) {
                                    tmpStrs.add(strs.get(i));
                                }
                            }
                            adapter.updateData(tmpStrs);
                            int size = tmpStrs.size()>5?5:tmpStrs.size();
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600,ITEM_HEIGHT*size);
                            rv.setLayoutParams(lp);
                        }else{
                            adapter.updateData(strs);
                            int size = strs.size()>5?5:strs.size();
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600,ITEM_HEIGHT*size);
                            rv.setLayoutParams(lp);
                        }
                    }
                }
            }
        });

        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()!=0){
                    tmpStrs.clear();
                    for (int i = 0; i < strs.size(); i++) {
                        if (strs.get(i).contains(s)) {
                            tmpStrs.add(strs.get(i));
                        }
                    }
                    adapter.updateData(tmpStrs);
                }else{
                    adapter.updateData(strs);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i(TAG, "onFocusChange: et"+hasFocus);
                if(hasFocus){
                    if (!popupWindow.isShowing()) {
                        popupWindow.showAsDropDown(et);
                        if(et.getText().length()!=0){
                            tmpStrs.clear();
                            for (int i = 0; i < strs.size(); i++) {
                                if (strs.get(i).contains(et.getText())) {
                                    tmpStrs.add(strs.get(i));
                                }
                            }
                            adapter.updateData(tmpStrs);
                            int size = tmpStrs.size()>5?5:tmpStrs.size();
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600,ITEM_HEIGHT*size);
                            rv.setLayoutParams(lp);
                        }else{
                            adapter.updateData(strs);
                            int size = strs.size()>5?5:strs.size();
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600,ITEM_HEIGHT*size);
                            rv.setLayoutParams(lp);
                        }
                    }
                }
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
            return;
        }
        super.onBackPressed();
    }
}
